package xyz.syyrjx.provider.handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import xyz.syyrjx.response.SyyrjxRpcResponse;
import xyz.syyrjx.code.SyyrjxRpcCoder;
import xyz.syyrjx.provider.exception.SyyrjxRpcProviderException;

/**
 * @author 大地崩坏苍蝇兽
 * @date 2023/1/12 - 16:31
 */
public class SyyrjxRpcProviderClientHandler extends SimpleChannelInboundHandler<String> {

    private final static Logger LOGGER = LoggerFactory.getLogger(SyyrjxRpcProviderClientHandler.class);

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, String str) throws Exception {
        SyyrjxRpcResponse response = SyyrjxRpcCoder.StringToObject(str);
        if (!response.isFlag()) {
            throw new SyyrjxRpcProviderException(SyyrjxRpcProviderException.PROVIDER_REGISTRY_ERROR);
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        LOGGER.error(cause.getMessage());
    }
}
